<?php

////////////////////////////////////////////////////////////////////////////////////////////////////
//image information
////////////////////////////////////////////////////////////////////////////////////////////////////

    class CImageInfo
    {	
	/* to be used with FastCheckImgType() */
	var $tImgJpg = IMAGETYPE_JPEG;
	var $tImgPng = IMAGETYPE_PNG;
	var $tImgGif = IMAGETYPE_GIF;
	var $tImgBmp = IMAGETYPE_BMP;
	/* eow */
    
	static function &FastCheckImgType($szLocation)
	{
	    $iType =  exif_imagetype($szLocation);
	}
	
	static function GetTypeAndSize($szLocation,&$iWidth = 0,&$iHeight = 0,&$szType = 0)
	{
	    list($iTWidth,$iTHeight,$iTType,$iTAttr) = getimagesize($szLocation);
	    
	    $iWidth  = $iTWidth;
	    $iHeight = $iTHeight;
	    
	    switch($iTType)
	    {
	    case 1:
		$szType = 'gif';
		break;
		
	    case 2:
		$szType = 'jpg';
		break;
		
	    case 3: 
		$szType = 'png';
		break;
		
	    case 6:
		$szType = 'bmp';
		break;
		
	    case 7:
		$szType = 'tif';
		break;
		
	    default:
		$szType = '';
	    }
	}
    };

////////////////////////////////////////////////////////////////////////////////////////////////////
//image transform - mainly for thumbnails
////////////////////////////////////////////////////////////////////////////////////////////////////

    
    class CImageTransform
    {
    
	static function Resize($szPath,$szDestPath,$iMaxW,$iMaxH)
	{
	    if( !is_file($szPath) ) return false;
	    
	    list($iSrcW,$iSrcH,$iTType) = getimagesize($szPath);
	    
	    
	    switch($iTType)
	    {
	    case 1:
		//gif
		$hImg = @imagecreatefromgif($szPath); 
		
		break;
		
	    case 2:
		//jpg
		$hImg = @imagecreatefromjpeg($szPath);
		break;
		
	    case 3: 
		//png
		$hImg = @imagecreatefrompng($szPath);
		break;	
		
	    default:
		return false;
	    }
	    	    

	    if(!$hImg) return false;
	
	    

	    if($iSrcW > $iSrcH)
	    {
		$iNewW = $iMaxW;
		$iNewH = $iMaxW * ($iSrcH / $iSrcW);
		
		if($iNewH > $iMaxW)
		{
		    $iNewH = $iMaxW;
		    $iNewW = $iMaxW * ($iSrcW /$iSrcH);
		}
		
		//$iOffsetH = 0;
	    }
	    else if($iSrcW < $iSrcH)
	    {
		$iNewH = $iMaxW;
		$iNewW = $iMaxW * ($iSrcW / $iSrcH);
		
		if($iNewW > $iMaxW)
		{
		    $iNewW = $iMaxW;
		    $iNewH = $iMaxW * ($iSrcH / $iSrcW);
		}
	    }
	    else
	    {
		$iNewW = $iMaxW;
		$iNewH = $iMaxH;
	    }
	    
	    //creating new image
	    $hTempImg = imagecreatetruecolor($iNewW,$iNewH);
	    
	    //enable image interlace
	    imageinterlace($hTempImg,0);
	    
	    imagecopyresampled($hTempImg,$hImg,0,0,0,0,$iNewW,$iNewH,$iSrcW,$iSrcH);
	    
	    if( imagejpeg($hTempImg,$szDestPath,60) == FALSE) return false;
	    
	    
	    imagedestroy($hTempImg);
	    
	    imagedestroy($hImg);
	    
	    //die();
	    
	    return true;
	}
    
	static function ResizeOld($szPath,$szDestPath,$iNewX,$iNewY)
	{
	    if( !is_file($szPath) ) return false;
	    
	    list($iTWidth,$iTHeight,$iTType,$iTAttr) = getimagesize($szPath);
	    
	    
	    switch($iTType)
	    {
	    case 1:
		//gif
		$hImg = @imagecreatefromgif($szPath); 
		break;
		
	    case 2:
		//jpg
		$hImg = @imagecreatefromjpeg($szPath);
		break;
		
	    case 3: 
		//png
		$hImg = @imagecreatefrompng($szPath);
		break;	
		
	    default:
		return false;
	    }
	    	    

	    if(!$hImg) return false;
	    
	    //obtaining img params
	    $iX = imagesx($hImg);
	    $iY  = imagesy($hImg);
	    
	    $iNewY = round( ($iY*$iNewX) / $iX);
	    
	    //creating new image
	    $hTempImg = imagecreatetruecolor($iNewX,$iNewY);
	    
	    //enable image interlace
	    imageinterlace($hTempImg,0);
	    
	    
	    imagecopyresampled($hTempImg,$hImg,0,0,0,0,$iNewX,$iNewY,$iX,$iY);
	    
	    if( imagejpeg($hTempImg,$szDestPath,60) == FALSE) return false;
	    
	    
	    imagedestroy($hTempImg);
	    
	    imagedestroy($hImg);
	    
	    return true;
	}
	
	static function ToJpg($szPath)
	{
	    if( !is_file($szPath) ) return false;
	
	    list($iTWidth,$iTHeight,$iTType,$iTAttr) = getimagesize($szPath);
	    
	    
	    switch($iTType)
	    {
	    case 1:
		//gif
		$hImg = @imagecreatefromgif($szPath); 
		break;
		
	    case 2:
		//jpg
		$hImg = @imagecreatefromjpeg($szPath);
		break;
		
	    case 3: 
		//png
		$hImg = @imagecreatefrompng($szPath);
		break;	
		
	    default:
		return false;
	    }
	
	    imageinterlace($hImg,0);
	
	    if( imagejpeg($hImg,$szPath,70) == FALSE ) 
	    {
		return false;
	    }
	    
	    imagedestroy($hImg);
	    
	    return true;
	}
    }
    
?>